package com.zhou.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhou.dao.MemberDao;
import com.zhou.entity.PageResult;
import com.zhou.entity.Result;
import com.zhou.pojo.Member;
import com.zhou.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    MemberDao memberDao;

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    public void delete(Integer id) {
        long countByCheckItemId = memberDao.findCountByCheckMemberId(id);
        if (countByCheckItemId > 0) {
            //当前检查项被引用，不能删除
            throw new RuntimeException("被引用，不能删除");
        }
        memberDao.deleteById(id);
    }


    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    @Override
    public String phoneValidator(Member member) {
        //获取表单提交的手机号
        String phoneNumber = member.getPhoneNumber();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前手机号是否已经被注册
        Integer phone = memberDao.findByPhone(phoneNumber);
        //通过表单的id查询旗下手机号与表单提交的手机号进行对比
        Member byId = memberDao.findById(id);
        if (phone > 0 && id == null) {
            return "手机号已经存在";
        } else if (id != null && !byId.getPhoneNumber().equals(phoneNumber) && phone != 0) {
            return "手机号已经存在";
        } else {
            return "恭喜您通过验证";
        }
    }

    @Override
    public String idCardValidator(Member member) {
        //获取表单提交的身份证号
        String idCard = member.getIdCard();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前身份证号是否已经被注册
        Integer byIdCard = memberDao.findByIdCard(idCard);
        //通过表单的id查询旗下身份证号与表单提交的身份证号进行对比
        Member byId = memberDao.findById(id);
        if (byIdCard > 0 && id == null) {
            return "身份证已经存在";
        } else if (id != null && !byId.getIdCard().equals(idCard) && byIdCard != 0) {
            return "身份证已经存在";
        } else {
            return "恭喜您通过验证";
        }
    }

    @Override
    public String fileNumberValidator(Member member) {
        //获取表单提交的档案号
        String fileNumber = member.getFileNumber();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前档案号已经被使用
        Integer byFileNumber = memberDao.findByFileNumber(fileNumber);
        //通过表单的id查询旗下档案号与表单提交的档案号进行对比
        Member byId = memberDao.findById(id);
        if (byFileNumber > 0 && id == null) {
            return "档案号已经存在";
        } else if (id != null && !byId.getFileNumber().equals(fileNumber) && byFileNumber != 0) {
            return "档案号已经存在";
        } else {
            return "恭喜您通过验证";
        }
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String>  month) {
        List<Integer> list = new ArrayList<>();
        for(String m : month){
            m = m + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);

    }
}
